package com.unytech.wisdomcommunity.view;

import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.unytech.wisdomcommunity.R;
import com.unytech.wisdomcommunity.application.MyApplication;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

/**
 * Created by zed on 2016/11/19/0019.
 *
 * 点击上传照片,弹出的"拍照","从相册选择","取消"的对话框
 * */
public class ShowPhotoGalleryDialog extends Dialog implements View.OnClickListener {

    private TextView mGalleryTv,mTakePhotoTv,mCalcelTv;
    private Uri imageUri;

    public static final int TAKE_PHOTO = 1;
    public static final int CROP_PHOTO = 2;

    private Fragment mFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_show_photo_gallery);//设置dialog的布局文件

        //获取窗体对象
        Window window = getWindow();
        //获取布局参数
        WindowManager.LayoutParams attributes = window.getAttributes();
        //设置重心  在底部
        attributes.gravity = Gravity.BOTTOM;
        //设置到窗体对象上去
        window.setAttributes(attributes);

        initView();
    }

    private void initView() {
        mGalleryTv = ((TextView) findViewById(R.id.dialog_gallery_tv));
        mTakePhotoTv = ((TextView) findViewById(R.id.dialog_take_photo_tv));
        mCalcelTv = ((TextView) findViewById(R.id.dialog_cancel_tv));

        mGalleryTv.setOnClickListener(this);
        mTakePhotoTv.setOnClickListener(this);
        mCalcelTv.setOnClickListener(this);
    }

    public ShowPhotoGalleryDialog(Context context, Fragment fragment) {
        //在一个参数的构造方法里调用父类两个参数的构造方法  设置主题样式
        //super(context);
        super(context, R.style.ShowPhotoGalleryDialogTheme);
        mFragment= fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_gallery_tv://打开相册
                break;
            case R.id.dialog_take_photo_tv://打开照相机
            {
                Calendar calendar =Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);
                int hour_of_day = calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);
                int second = calendar.get(Calendar.SECOND);

                File imageFile = new File(Environment.getExternalStorageDirectory(),
                        year+month+day_of_month+hour_of_day+minute+second+".jpg");

                try {
                    if (imageFile.exists()){
                        imageFile.delete();
                    }
                    imageFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                imageUri = Uri.fromFile(imageFile);

                Toast.makeText(MyApplication.getAppContext(),"点击了拍照",Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                mFragment.startActivityForResult(intent,TAKE_PHOTO);//点击开启手机相机程序
            }
                break;
            case R.id.dialog_cancel_tv://取消
                dismiss();
                break;
        }
    }
}